;(function(root) {
    // window.baseUrl = "http://192.168.0.109:8080/spterp/"; //路径
    // window.baseUrl = "http://192.168.0.56:8080/erpbuild/"; //路径
    window.baseUrl = ""; //路径


    // window.baseUrl = "http://gcjs.hntxrj.com";
    var timeoutTime = 40000; //超时 40秒

    root.CODEMESSAGE = {
        ERRORCOD : 0, //后台返回数据失败
        SUCCESSCODE : 0 //成功的code值
    }
    root.getCookie=function (name)
    {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    }
    root.rose = {

        getUrl : baseUrl,

        util : {
            stopScroll : function(){
                document.body.style.overflow='hidden';
            },
            runScroll : function(){

                document.body.style.overflow='';
            },
            addHash: function () {

                // exit if the browser implements that event
                if ("onhashchange" in window) { return; }

                var location = window.location,
                    oldURL = location.href,
                    oldHash = location.hash;

                // check the location hash on a 100ms interval
                setInterval(function() {
                    var newURL = location.href,
                        newHash = location.hash;

                    // if the hash has changed and a handler has been bound...
                    if (newHash != oldHash && typeof window.onhashchange === "function") {
                        // execute the handler
                        window.onhashchange({
                            type: "hashchange",
                            oldURL: oldURL,
                            newURL: newURL
                        });

                        oldURL = newURL;
                        oldHash = newHash;
                    }
                }, 100);

            },
            getReauest : function() {
               var url = location.search; //获取url中"?"符后的字串
               var theRequest = new Object();
               if (url.indexOf("?") != -1) {
                  var str = url.substr(1);
                  strs = str.split("&");
                  for(var i = 0; i < strs.length; i ++) {
                     theRequest[strs[i].split("=")[0]]=decodeURIComponent(strs[i].split("=")[1]);
                  }
               }
               return theRequest;
           },
           getStates : [
               {
                    name : "全部"
               },{
                    name : "等待生产",
                    code : 0
               },{
                    name : "正在生产",
                    code : 1
               },{
                   name : "暂停",
                   code : 2
               },{
                   name : "完成",
                   code : 3
               },{
                   name : "删除",
                   code : 4
               }
            ],
            getJavaTime: function (str) {
                return this.getPhoneTime(str)
            },
            getH5Time: function (str) {
                return str ? new Date(str.time).Format('yyyy-MM-ddThh:mm') : '';
            },
            getH5Time1: function (str) {
                return str ? new Date(str.time).Format('yyyy-MM-dd') : '';
            },
            setItem:function (key,value) {
                if(window.localStorage){
                    window.localStorage.setItem(key,value);
                }else{
                    $.fn.cookie(key,value,{
                        path: '/',
                        expires : 10000000
                    });
                }
            },
            getItem:function (key) {
                if(window.localStorage){
                    return window.localStorage.getItem(key);
                }else{
                    return $.fn.cookie(key);
                }
            },
            removeItem:function (key) {
                if(window.localStorage){
                    window.localStorage.removeItem(key);
                }else{
                    $.fn.cookie(key,'',{
                        path: '/',
                        expires : -10000000
                    });
                }
            },
            getIndexDay : function(AddDayCount) {
                function getDateStr(dayCount){
                    if(null == dayCount){
                        dayCount = 0;
                    }
                    var dd = new Date();
                    dd.setDate(dd.getDate()+dayCount);//设置日期
                    var y = dd.getFullYear();
                    var m = dd.getMonth()+1;//获取当前月份的日期
                    var d = dd.getDate();

                    dd.setHours(0);
                    dd.setMinutes(0);
                    dd.setSeconds(0);

                    return dd
                }
                // var dd = new Date();
                // dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
                // var y = dd.getFullYear();
                // var m = dd.getMonth()+1;//获取当前月份的日期
                // var d = dd.getDate();
                return  new Date(getDateStr(AddDayCount)).Format('yyyy-MM-dd hh:mm:ss');
            },
            getPhoneTime: function (str) {

                var t = str.replace(/[T|+]/,' ') || '';
                var sub = t.indexOf('.');

                if(sub != -1){
                    t = t.substr(0,sub);
                }

                return t || '';

            },
            getTodayBeginTime: function () {
                return this.getIndexDay(0)
            },
            getTodayEndTime: function () {
                function getDateStr(dayCount){
                    if(null == dayCount){
                        dayCount = 0;
                    }
                    var dd = new Date();
                    dd.setDate(dd.getDate()+dayCount);//设置日期
                    var y = dd.getFullYear();
                    var m = dd.getMonth()+1;//获取当前月份的日期
                    var d = dd.getDate();

                    dd.setHours(23);
                    dd.setMinutes(59);
                    dd.setSeconds(59);

                    return dd
                }
                // var dd = new Date();
                // dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
                // var y = dd.getFullYear();
                // var m = dd.getMonth()+1;//获取当前月份的日期
                // var d = dd.getDate();

                return  new Date(getDateStr()).Format('yyyy-MM-dd hh:mm:ss');
            }

        }
    };
    function extender(p,c) {
        for(var i in c){
            c[i] = p[i]
        }
    }
    // 新版成功状态码判断
    root.STATES = {
        SUCCESS : 0, //成功状态 为0
        isSuccess : function (data,success,error) {
            if(data.code == this.SUCCESS){
                if(typeof data.data === "object"){
                    // 数据正常 状态码正常的 成功
                    success(data);
                }else{
                    //状态码正常 数据不正常的 失败
                    error(data);
                }

            }else{
                //状态码正常的 失败
                error(data);
            }
        },
        ioError : function (root,data) {
            root.console.log(data && data.msg || "获取数据失败");
        },
        netError : function () {
            return "获取数据失败";
        },
        outCode : function (msg) {
            var msger = "";
            switch (msg){
                case "重复":
                    msger = "添加数据重复";
                    break;
                case "成功":
                    msger = "操作成功";
                    break;
                case "失败":
                    msger = "操作失败";
                    break;
            }


            return msger ? msger : msg;
        }
    }


    function extender(p,c){
        for(var i in c){
            p[i] = c[i];
        }

        return p;
    }

    // 通用get ajax 方式
    function sendAjax(url,json,success,error,copyer){

        json.compid = rose.util.getItem('compid');
        json.opid = rose.util.getItem('opid');
        json.token = rose.util.getItem('token');
        json.empname = rose.util.getItem('empname');

        json.admin_compid = rose.util.getItem('admin_compid');
        json.admin_opid = rose.util.getItem('admin_opid');
        json.admin_token = rose.util.getItem('admin_token');
        json.admin_empname = rose.util.getItem('admin_empname');



         $.ajax(extender({
            url : baseUrl+url,
            data : json,
            success : function(data, textStatus, request){
                if (request.getAllResponseHeaders().indexOf('REDIRECT') != -1){

                    //这里的重定向后期要用到
                    document.location.href = window.baseUrl+"login1.html";
                    rose.util.setItem('jumpType','REDIRECT')
                    console.log('jump')
                }
                success(data);

            },
            timeout : timeoutTime,
            async: true,
            error : function(xhr,key){
                error && error();
                if(key == "timeout"){
                    try{
                        layer.closeAll();
                        layer.open({
                            content: "请求数据超时",
                            skin: 'msg',
                            time: 4
                        });
                    }catch(e){}
                }
            }
        },copyer || {}))

    }

    // 通用post 方式
    function postAjax(url,json,success,error,copyer){
        json.compid = rose.util.getItem('compid');
        json.opid = rose.util.getItem('opid');
        json.token = rose.util.getItem('token');
        json.empname = rose.util.getItem('empname');

        json.admin_compid = rose.util.getItem('admin_compid');
        json.admin_opid = rose.util.getItem('admin_opid');
        json.admin_token = rose.util.getItem('admin_token');
        json.admin_empname = rose.util.getItem('admin_empname');


        $.ajax(extender({
            url : baseUrl+url,
            type : "post",
            data : json,
            success : function(data, textStatus, request){
                if (request.getAllResponseHeaders().indexOf('REDIRECT') != -1){

                    //这里的重定向后期要用到
                    document.location.href = window.baseUrl+"login1.html";
                    rose.util.setItem('jumpType','REDIRECT')
                    console.log('jump')
                }
                success(data);
            },
            beforeSend: function () {
               // console.log(arguments,'<--------')
            },
            timeout : timeoutTime,
            async: true,
            error : function(xhr,key){
                error && error();
                if(key == "timeout"){
                    try{
                        layer.closeAll();
                        layer.open({
                            content: "请求数据超时",
                            skin: 'msg',
                            time: 4
                        });
                    }catch(e){}
                }
            }
        },copyer || {}))


    }

    // 暴露在全局的 api
    // 项目地址详见 http://apizza.cc/

    root.API = {
        logOut : function (fn) { // 退出登录
            if(window.localStorage){
                localStorage.removeItem('compid');
                localStorage.removeItem('opid');
                localStorage.removeItem('token');
                localStorage.removeItem('empname');
            }else{
                var config = {
                    path: '/',
                    expires : -10000000 //七天
                }

                $.fn.cookie('opid','',config);
                $.fn.cookie('token','',config);
                $.fn.cookie('empname','',config);
                $.fn.cookie('api','',config);
            }


            fn && fn();
        },

       
        loadPermi : function (json,success,error) { // 加载权限组/用户组-用户组详情-含分页
            postAjax('/api/permi/loadPermi.do',json,success,error) 
        },
    
        adminLoadMenu : function (json,success,error) { // 加载所有菜单
            postAjax('/api/permi/loadMenu.do',json,success,error)
        },
        adminLoadPermiSelected : function (json,success,error) { //获取已有权限ID
            postAjax('/api/permi/loadPermiSelected.do',json,success,error)
        },
        adminSavePermiss : function (json,success,error) {//admin 状态存
            postAjax('/api/permi/savePermissValue.do',json,success,error,{
                'processData': false,
                'contentType': 'application/json'
            })
        },
        editPermiGroup: function (json,success,error) { // 权限组 增-删-改
            postAjax('/api/permi/delPermi.do',json,success,error)
        }
    

    }
})(window)
